g_NotepadTable = {};			-- Save Craft Skill info each time it changes
local g_NotepadStatus = {};
local PLUGIN_TITLE = "Notepad - THX to The Gooch"
--local TABLE_DATA_FILENAME = "Notepad.ini"

local g_CurRelamPlayer = "Default";

function Log(msg)
	if (DEFAULT_CHAT_FRAME) then
		DEFAULT_CHAT_FRAME:AddMessage(msg);
	end
end


--
-- This function will strip the first color it see's in the TextToStrip
--
function StripColor( TextToStrip )

	--Log( "Before stripping color: \"" ..TextToStrip.. "\"" );
	local stripped = TextToStrip;

	while ( string.find( stripped, "|c" ) ~= nil ) do
		stripped = string.gsub( stripped, "|c%x%x%x%x%x%x%x%x", "" );
		stripped = string.gsub( stripped, "|r", "" );
	end
	
	--Log( "After stripping color: \"" ..stripped.. "\"" );

	return stripped;
end


function Notepad_LoadTable()

	--Log("Notepad_LoadTable - loading " .. TABLE_DATA_FILENAME);

	--local IniName=GC_GetGlobalPath(TABLE_DATA_FILENAME);
	--local tableIni=table.load(IniName);
	--table.copy(g_NotepadTable,tableIni);	
	--Log("Loaded " .. TABLE_DATA_FILENAME );
end


function Notepad_SaveTable()

	--Log("Notepad_SaveTable - saving " .. TABLE_DATA_FILENAME);
	
	--local IniName=GC_GetGlobalPath(TABLE_DATA_FILENAME);
	--table.save(g_NotepadTable,IniName);
	SaveVariables("g_NotepadTable");
	--Log("Saved " .. TABLE_DATA_FILENAME );
end


function IsNotepadInitialized()
	
	if ( g_NotepadStatus.Initialized == true ) then
		return true;
	end

	return false;
end


function SetNotepadInitialized()

	g_NotepadStatus.Initialized = true;
	--Log( "g_NotepadTable Initialized." );
end


function Initialize_Notepad()
	--Log("g_NotepadTable initializing...");

	-- Bail if we're already initialized
	if ( IsNotepadInitialized() == true ) then
		return;
	end

	-- Try to get the current player name.  If we can then we consider ourselves initialized
	local PlayerName = UnitName("player");

	if ( not PlayerName or PlayerName == "" ) then
		return;
	end
	
	g_CurRelamPlayer = StripColor( GetCurrentRealm().."/"..PlayerName );
	
	-- Initilaize our global data object if it isn't already
	if ( not g_NotepadTable[g_CurRelamPlayer] ) then 
		g_NotepadTable[g_CurRelamPlayer] = {}; 
	end

	-- Set flag saying we're initialized so following functions don't bail out
	SetNotepadInitialized();

	-- Populate the text
	if ( g_NotepadTable[g_CurRelamPlayer].Text and g_NotepadTable[g_CurRelamPlayer].Text ~= "" ) then
		NotepadFrameEditbox:SetText(g_NotepadTable[g_CurRelamPlayer].Text);
	else
		NotepadFrameEditbox:SetText("...");
	end
	
	-- Save the latest table
	Notepad_SaveTable();
end



SLASH_Notepad1 = "/notepad";
SLASH_Notepad2 = "/np";
SlashCmdList["Notepad"] = function (editBox, msg)

	ShowUIPanel(NotepadFrame);
	NotepadFrameEditbox:SetFocus();
end


function Notepad_OnLoad(this)

	NotepadFrameTitleText:SetText(PLUGIN_TITLE);
	
	-- Register for Events we care about
	this:RegisterEvent("UNIT_PORTRAIT_UPDATE");
	this:RegisterEvent("VARIABLES_LOADED");
	
end


function Notepad_OnShow(this)

	--Log("Notepad_OnShow");
	this:ResetFrameOrder();
end


function Notepad_OnHide(this)
	
	--Log("Notepad_OnHide");
	
	-- Save on close
	Notepad_SaveTable();

	this:Hide();
end


function Notepad_OnEvent(this, event)
	
	--Log("Notepad Event: " .. event);
	--if ( arg1 ) then Log("arg1: " .. arg1); end
	--if ( arg2 ) then Log("arg2: " .. arg2); end
	
	-- Event: VARIABLES_LOADED
	-- we load our .INI file here
	if ( event == "VARIABLES_LOADED" ) then
		--Notepad_LoadTable();

        -- If the user does a /script ReloadUI() only this event will be called.
		if ( IsNotepadInitialized() ~= true ) then
			Initialize_Notepad();
		end

		return;
	end


	-- Event: UNIT_PORTRAIT_UPDATE
	-- We Get the player name here and Initialize our AddOn
	if ( event == "UNIT_PORTRAIT_UPDATE" and arg1 == "player" ) then
		
		if ( IsNotepadInitialized() ~= true ) then
			Initialize_Notepad();
		end
		return;
	end
end


function Notepad_EditboxTextChanged(this)

	--Log( "Notepad_EditboxTextChanged" );

	if ( g_CurRelamPlayer == "Default" ) then
	
		-- Try to get the current player name.  If we can then we consider ourselves initialized
		local PlayerName = UnitName("player");

		if ( PlayerName == "" ) then
			return;
		end
		
		g_CurRelamPlayer = StripColor( GetCurrentRealm().."/"..PlayerName );
	end
	
	
	if ( not g_NotepadTable[g_CurRelamPlayer] ) then 
		g_NotepadTable[g_CurRelamPlayer] = {}; 
	end

	g_NotepadTable[g_CurRelamPlayer].Text = this:GetText();
end
